package com.java.digitalsignagesystem.service.impl;

import com.java.digitalsignagesystem.entity.Event;
import com.java.digitalsignagesystem.mapper.EventMapper;
import com.java.digitalsignagesystem.service.EventService;
import com.java.digitalsignagesystem.utils.RedisUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * (Event)表服务实现类
 *
 * @author makejava
 * @since 2024-02-29 15:51:44
 */
@Service("eventService")
public class EventServiceImpl implements EventService {
    @Value("${vpc.ip}")
    private String vpcIp;

    @Value("${vpc.port}")
    private String vpcPort;

    @Autowired
    RedisUtil redisUtil;
    @Resource
    private EventMapper eventMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Event queryById(Integer id) {
        return this.eventMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param event 实例对象
     * @return 实例对象
     */
    @Override
    public Event insert(Event event) {
        this.eventMapper.insert(event);
        return event;
    }

    /**
     * 修改数据
     *
     * @param event 实例对象
     * @return 实例对象
     */
    @Override
    public Event update(Event event) {
        this.eventMapper.update(event);
        return this.queryById(event.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.eventMapper.deleteById(id) > 0;
    }

    @Override
    public Event getByChooseIndex(String chooseIndex) throws ParseException {
        return eventMapper.selectToday(chooseIndex);
    }

    @Override
    public String getGjzs(String sourceDeptName) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://"+vpcIp+":"+vpcPort+"/service/api/zp/gjzs/new?sourceDeptName="+sourceDeptName)
                .get()
                .addHeader("content-type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            return response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> getChooseIndexList() {
       return eventMapper.selectChooseIndexToday();
    }


}
